/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sisfo.astacala.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 * @author arif
 */
public class UtilGeneral {

    private static UtilGeneral utilGeneral;
    public static String exportCsvPath = "d://exportcsv//";

    public static UtilGeneral getInstance() {
        if (utilGeneral == null) {
            utilGeneral = new UtilGeneral();
        }
        return utilGeneral;
    }
    
    public String getCurrentDate(String format){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(new Date());
    }
    
    public String generateMenu(String parentId) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
//            String queryParent = String.format("from ObjectMenu where caption='%s'", parentId);
            String queryParent = String.format("SELECT * FROM tbls_menu WHERE id IN (SELECT menuid FROM tbls_menupermission WHERE permissionfor='%s') AND ("
                    + "(parent IS NULL) OR (parent='') )",
                    parentId);
//            String queryChild = String.format("from ObjectMenu where permission='%s'", parentId);

            UtilDatabase.getInstance().openConnection();
            List<Map> parentMenu = UtilDatabase.getInstance().executeQuery(queryParent, new ArrayList());

            String nodeTemplate = "{ \"data\" : {\"title\" : \"%s\", \"attr\" : { \"href\" : \"%s\"} } }";

            stringBuilder.append("[");
            //home
            stringBuilder.append(String.format(nodeTemplate, "Home", "javascript:change_content('home.jsp')"));
            stringBuilder.append(",");

            // core menu
            for (int i = 0; i < parentMenu.size(); i++) {
                stringBuilder.append("{\"data\" : \"").append(parentMenu.get(i).get("caption")).append("\",");
                stringBuilder.append(String.format(" \"href\" : \"%s\"", parentMenu.get(i).get("url")));

                String queryChild = String.format("SELECT * FROM tbls_menu WHERE id IN (SELECT menuid FROM tbls_menupermission WHERE permissionfor='%s') AND parent='%s'",
                        parentId,
                        parentMenu.get(i).get("id"));
                List<Map> childMenu = UtilDatabase.getInstance().executeQuery(queryChild, new ArrayList());
                stringBuilder.append(", \"children\" : [");
                for (int j = 0; j < childMenu.size(); j++) {
                    if (j != 0) {
                        stringBuilder.append(",");
                    }

                    stringBuilder.append(String.format(nodeTemplate, childMenu.get(j).get("caption"), childMenu.get(j).get("url")));
                }
                stringBuilder.append("]},");
            }

            // logout
            stringBuilder.append(String.format(nodeTemplate, "Logout", "javascript:logout()"));
            stringBuilder.append("]");
            UtilDatabase.getInstance().closeConnection();
        } catch (Exception ex) {
            ex.printStackTrace();
            UtilDatabase.getInstance().closeConnection();
        }
        return stringBuilder.toString();
    }

    public static void main(String args[]) {
        System.out.println(UtilGeneral.getInstance().generateMenu("Sekretariat"));
    }
}